home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.5)
-
- __revision__ = '$Id: jid.py 648 2006-08-26 20:09:37Z jajcus $'
- __docformat__ = 'restructuredtext en'
- import re
- import weakref
- import warnings
- from encodings import idna
- from pyxmpp.xmppstringprep import nodeprep, resourceprep
- from pyxmpp.exceptions import JIDError
- node_invalid_re = re.compile(u'["&\'/:<>@\\s\\x00-\\x19]', re.UNICODE)
- resource_invalid_re = re.compile(u'[\\s\\x00-\\x19]', re.UNICODE)
-
- def are_domains_equal(a, b):
- a = idna.ToASCII(a)
- b = idna.ToASCII(b)
- return a.lower() == b.lower()
-
-
- class JID(object):
- cache = weakref.WeakValueDictionary()
- __slots__ = [
- 'node',
- 'domain',
- 'resource',
- '__weakref__']
-
- def __new__(cls, node_or_jid = None, domain = None, resource = None, check = True):
- if isinstance(node_or_jid, JID):
- return node_or_jid
-
- if domain is None and resource is None:
- obj = cls.cache.get(node_or_jid)
- if obj:
- return obj
-
- else:
- obj = None
- if obj is None:
- obj = object.__new__(cls)
-
- if node_or_jid:
- node_or_jid = unicode(node_or_jid)
-
- if node_or_jid:
- if u'@' in node_or_jid or u'/' in node_or_jid:
- obj._JID__from_unicode(node_or_jid)
- cls.cache[node_or_jid] = obj
- elif domain is None and resource is None:
- if node_or_jid is None:
- raise JIDError, 'At least domain must be given'
-
- domain = node_or_jid
- node_or_jid = None
-
- if check:
- obj._JID__set_node(node_or_jid)
- obj._JID__set_domain(domain)
- obj._JID__set_resource(resource)
- else:
- object.__setattr__(obj, 'node', node_or_jid)
- object.__setattr__(obj, 'domain', domain)
- object.__setattr__(obj, 'resource', resource)
- return obj
-
-
- def __setattr__(self, name, value):
- raise RuntimeError, 'JID objects are immutable!'
-
-
- def __from_unicode(self, s, check = True):
- s1 = s.split(u'/', 1)
- s2 = s1[0].split(u'@', 1)
- if len(s2) == 2:
- if check:
- self._JID__set_node(s2[0])
- self._JID__set_domain(s2[1])
- else:
- object.__setattr__(self, 'node', s2[0])
- object.__setattr__(self, 'domain', s2[1])
- elif check:
- self._JID__set_domain(s2[0])
- else:
- object.__setattr__(self, 'domain', s2[0])
- object.__setattr__(self, 'node', None)
- if len(s1) == 2:
- if check:
- self._JID__set_resource(s1[1])
- else:
- object.__setattr__(self, 'resource', s1[1])
- else:
- object.__setattr__(self, 'resource', None)
- if not self.domain:
- raise JIDError, 'Domain is required in JID.'
-
-
-
- def __set_node(self, s):
- if s:
- s = unicode(s)
- s = nodeprep.prepare(s)
- if len(s.encode('utf-8')) > 1023:
- raise JIDError, 'Node name too long'
-
- else:
- s = None
- object.__setattr__(self, 'node', s)
-
-
- def __set_domain(self, s):
- if s is None:
- raise JIDError, 'Domain must be given'
-
- if s:
- s = unicode(s)
-
- s = idna.nameprep(s)
- if len(s.encode('utf-8')) > 1023:
- raise JIDError, 'Domain name too long'
-
- object.__setattr__(self, 'domain', s)
-
-
- def __set_resource(self, s):
- if s:
- s = unicode(s)
- s = resourceprep.prepare(s)
- if len(s.encode('utf-8')) > 1023:
- raise JIDError, 'Resource name too long'
-
- else:
- s = None
- object.__setattr__(self, 'resource', s)
-
-
- def __str__(self):
- return self.as_utf8()
-
-
- def __unicode__(self):
- return self.as_unicode()
-
-
- def __repr__(self):
- return '<JID: %r>' % self.as_unicode()
-
-
- def as_utf8(self):
- return self.as_unicode().encode('utf-8')
-
-
- def as_string(self):
- warnings.warn('JID.as_string() is deprecated. Use unicode() or `as_utf8` instead.', DeprecationWarning, stacklevel = 1)
- return self.as_utf8()
-
-
- def as_unicode(self):
- r = self.domain
- if self.node:
- r = self.node + u'@' + r
-
- if self.resource:
- r = r + u'/' + self.resource
-
- if not JID.cache.has_key(r):
- JID.cache[r] = self
-
- return r
-
-
- def bare(self):
- return JID(self.node, self.domain, check = False)
-
-
- def __eq__(self, other):
- if other is None:
- return False
- elif type(other) in (str, unicode):
-
- try:
- other = JID(other)
- return False
-
- elif not isinstance(other, JID):
- return False
-
- if self.node == other.node and are_domains_equal(self.domain, other.domain):
- pass
- return self.resource == other.resource
-
-
- def __ne__(self, other):
- return not self.__eq__(other)
-
-
- def __cmp__(self, other):
- a = self.as_unicode()
- return cmp(a, other)
-
-
- def __hash__(self):
- return hash(self.node) ^ hash(self.domain) ^ hash(self.resource)
-
-
-